/**
 * 竞争分析
 * @author miaoyl
 * @date 2018/01/14
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_where = {},
        cols = [[
            {type: 'checkbox', fixed: 'left'},
            {field: 'GWGG001', width: 160, title: '招标编号', fixed: 'left', sort: true,
                search: 'default',searchOpts: {icon:true}}
            , {field: 'GWGC022', width: 160, title: '项目名称',search: 'default'}
            , {field: 'GWGG002', width: 160, title: '竞争厂商', sort:true,
                search: 'default',searchOpts: {icon:true}}
            , {field: 'GWGG003', width: 160, title: '地址', sort:true}
            , {field: 'GWGG004', width: 160, title: '竞争程度', sort:true,
                filterDatas: app.datas_('crm_gwgi004_gwgg004'),
                search: 'default', searchOpts: {type: 'select'}
            }
            , {field: 'GWGGORIU_NAME', width: 160, title: '业务员',search: 'default',searchOpts: {icon:true}}
            , {field: 'GWGGORIG_NAME', width: 160, title: '所属组织'},
            {
                width: 60,
                title: '',
                toolbar: '#script-gwgi004setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwgi004SettingAction,
        moduleName = '竞争分析'
    ;

    var Gwgi004 = app.Module.extend({
        init: function () {
            self = this;
            var _searchsArr = app.cloneJSON(cols[0]);
            self._super({
                _tag: 'gwgi004',
                _searchs: [{
                    id: 'gwgi004', tableId: 'gwgi004_table', datas: cols[0]
                }],
                _loadTplsFull: [
                    {
                        dom: 'common_open_window', tpl: 'common/_open_window'
                    },
                    {
                        dom: 'gwgi004_edit', tpl: 'gwgi004_edit'
                    }
                ]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwgi004_table'),
                    url: g_ctxPath + 'crm/gwgi004/getGwgglist',
                    exportsUrl: g_ctxPath + 'crm/gwgi004/getGwgglist',
                    exportsName: moduleName + '数据',
                    title: moduleName + '信息表',
                    method: 'post',
                    id: 'gwgi004_table',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: '',
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwgi004setting-action').innerHTML).render({}, function (string) {
                    gwgi004SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwgi004_table)', function (obj) {

                    layer.tips(gwgi004SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'gwgi004_edit',
                                            tagData: {
                                                obj: obj
                                            },
                                            index: 1,
                                            openOpts: {
                                                title: moduleName + '编辑',
                                                maxmin: true
                                            }
                                        });
                                        break;
                                    case '1':
                                        self.delData(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwgi004_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwgi004_edit',
                        tagData: {
                            obj: obj,
                            is_detail:true
                        },
                        index: 1,
                        openOpts: {
                            title: moduleName + '详情',
                            maxmin: true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwgi004_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwgi004_edit',
                                tagData: {
                                    is_add: true
                                },
                                index: 1,
                                openOpts: {
                                    title: moduleName + '编辑',
                                    maxmin: true
                                }
                            });
                            break;
                        case 'del':
                            app.confirm({
                                msg: '是否删除选择的数据？',
                                ok: function () {
                                    self.delDataBatch();
                                }
                            });
                            break;
                        case 'valid' :
                            self.valid();
                            break;
                        case 'invalid' :
                            self.invalid();
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwgi004_table', where: table_where});
        },

        delData: function (obj) {
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'crm/gwgi004/del',
                        xdata: obj.data,
                        loading: true,
                        okmsg: moduleName + '删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            });

        },

        delDataBatch: function () {
            var checkStatus = table.checkStatus('gwgi004_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWGG_T');
                    _temp.GWGGID = v.GWGGID;
                    _temp.GWGGSEQ= v.GWGGSEQ;
                    _temp.GWGGENT = v.GWGGENT;
                    _xdata.push(_temp);
                });
                app.ajax({
                    xurl: 'crm/gwgi004/delDataBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    okmsg: moduleName + '删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        valid: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwgi004_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWGG_T');
                    if (v.GWGGACTI != 'N') {
                        app.showWarning("只有状态为N：无效才可以进行有效操作！");
                        flag = false;
                        return;
                    } else {
                        _temp.GWGGENT = v.GWGGENT;
                        _temp.GWGGID = v.GWGGID;
                        _temp.GWGGSEQ= v.GWGGSEQ;
                        _temp.GWGGACTI = "Y";
                        _xdata.push(_temp);
                    }
                });
            } else {
                app.showWarning("请先选择数据！");
                return;
            }
            if (flag) {
                app.confirm({
                    msg: '是否有效选择的数据？',
                    ok: function () {
                        app.ajax({
                            xurl: 'crm/gwgi004/updateStatus',
                            xdata: _xdata,
                            loading: true,
                            toJsonStr: true,
                            okmsg: moduleName + '有效成功',
                            cb: function (result) {
                                if (result.ok) {
                                    self.reloadTableCurr();
                                }
                            }
                        });
                    }
                });
            }
        },

        invalid: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwgi004_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWGG_T');
                    if (v.GWGGACTI != 'Y') {
                        app.showWarning("只有状态为Y：有效才可以进行无效操作！");
                        flag = false;
                        return;
                    } else {
                        _temp.GWGGENT = v.GWGGENT;
                        _temp.GWGGID = v.GWGGID;
                        _temp.GWGGSEQ= v.GWGGSEQ;
                        _temp.GWGGACTI = "N";
                        _xdata.push(_temp);
                    }
                });
            } else {
                app.showWarning("请先选择数据！");
                return;
            }
            if (flag) {
                app.confirm({
                    msg: '是否无效选择的数据？',
                    ok: function () {
                        app.ajax({
                            xurl: 'crm/gwgi004/updateStatus',
                            xdata: _xdata,
                            loading: true,
                            toJsonStr: true,
                            okmsg: moduleName + '无效成功',
                            cb: function (result) {
                                if (result.ok) {
                                    self.reloadTableCurr();
                                }
                            }
                        });
                    }
                });
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwgi004',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if(field == 'GWGG001'){ //招标编号
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwga001',
                                            columnList: [
                                                {label: '招标编号', prop: 'GWGA001', width: '150'},
                                                {label: '项目名称', prop: 'GWGA002', width: '150'},
                                                {label: '客户', prop: 'GWAKL004', width: '150'},
                                                {label: '开标时间', prop: 'GWAKL004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWGG001 = data.GWGA001;
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择招标信息',
                                        area: ['650px', '465px']
                                    }
                                });
                            }else if (field == 'GWGG002') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwgg002',
                                            columnList: [
                                                {label: '竞争厂商', prop: 'GWGG002', width: '150'},
                                                {label: '地址', prop: 'GWGG003', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWGG002 = data.GWGG002;
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择竞争厂商',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else if(field == 'GWGGORIU_NAME'){
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_sysuser_id',
                                            columnList: [
                                                {label: '账号', prop: 'ACCOUNT', width: '150'},
                                                {label: '姓名', prop: 'NAME', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWGGORIU_NAME = data.ID;
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择业务员',
                                        area: ['600px', '470px']
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new Gwgi004();

});